package com.edu.exam.enums.grading;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 质量等级枚举
 *
 * <p>管理质量评估的等级分类，包括优秀、良好、中等、差、不可接受等级。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-14
 */
public enum QualityGrade implements BaseEnum {

    /**
     * 优秀
     */
    EXCELLENT("EXCELLENT", "优秀"),

    /**
     * 良好
     */
    GOOD("GOOD", "良好"),

    /**
     * 中等
     */
    FAIR("FAIR", "中等"),

    /**
     * 差
     */
    POOR("POOR", "差"),

    /**
     * 不可接受
     */
    UNACCEPTABLE("UNACCEPTABLE", "不可接受");

    private final String code;
    private final String description;

    QualityGrade(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 根据编码查找枚举值
     *
     * @param code 业务编码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果编码无效
     */
    public static QualityGrade fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Quality grade code cannot be null or empty");
        }

        for (QualityGrade grade : values()) {
            if (grade.getCode().equals(code)) {
                return grade;
            }
        }
        throw new IllegalArgumentException("Unknown quality grade code: " + code);
    }

    /**
     * 根据描述查找枚举值
     *
     * @param description 显示描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static QualityGrade fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Quality grade description cannot be null or empty");
        }

        for (QualityGrade grade : values()) {
            if (grade.getDescription().equals(description)) {
                return grade;
            }
        }
        throw new IllegalArgumentException("Unknown quality grade description: " + description);
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}